import Router from 'koa-router'

import StudentModel from '../dbs/models/student'
import CBLModel from '../dbs/models/CBlate'
import informtModel from '../dbs/models/inform'

let router = new Router({ prefix: '/tower' })
// 获取对应tower全部数据
router.get('/towerList', async (ctx, next) => {
  let tower = { tower: ctx.query.tower }
  let page = Number(ctx.query.pagenum)//当前第几页
  let size = Number(ctx.query.pagesize)//每页显示的记录条数
  let options = { "limit": size, "skip": (page - 1) * size }
  let result = await StudentModel.find(tower, null, options).sort({ "dorm": 1 })
  let count = await StudentModel.find(tower, null)
  let total = count.length
  ctx.body = {
    total: total,
    studentlist: result,
    meta: {
      msg: "请求数据成功",
      code: 200
    }
  }
})
// 晚归上报
router.get('/CBlate', async ctx => {
  const addCBateStudent = new CBLModel({
    tower: ctx.query.tower,
    dorm: ctx.query.dorm,
    name: ctx.query.name,
    studentNum: ctx.query.studentNum,
    BCLtime: ctx.query.BCLtime,
    cause: ctx.query.cause
  })
  let code
  // console.log(ctx.query.BCLtime)
  try {
    await addCBateStudent.save()
    code = 0
  } catch (e) {
    code = -1
  }
  ctx.body = {
    msg: '添加成功',
    code: code
  }
})
// 查看晚归情况
router.get('/findCBlate', async ctx => {
  let where = {}
  let { tower, studentNum } = ctx.query
  if (tower) {
    where = {
      tower
    }
  } else if (studentNum) {
    where = { studentNum }
  }
  // console.log(ctx.query)
  let page = Number(ctx.query.pagenum)//当前第几页
  let size = Number(ctx.query.pagesize)//每页显示的记录条数
  let options = { "limit": size, "skip": (page - 1) * size }
  let result = await CBLModel.find(where, null, options).sort({ "BCLtime": -1 })
  let count = await CBLModel.find(where, null)
  let total = count.length
  if (result.length !== 0) {
    ctx.body = {
      total: total,
      studentlist: result,
      meta: {
        msg: "请求数据成功",
        code: 200
      }
    }
  } else {
    ctx.body = {
      total: total,
      studentlist: result,
      meta: {
        msg: "没有晚归学生",
        code: 200
      }
    }
  }
})
// 删除晚归信息
router.post('/delCBL', async ctx => {
  const delCBLstudent = { _id: ctx.request.body.id }
  // console.log(delCBLstudent)
  let code
  try {
    await CBLModel.remove(delCBLstudent, null)
    code = 200
  } catch (e) {
    code = -1
  }
  ctx.body = {
    msg: '删除成功',
    code: code
  }

})
// 添加通知
router.post('/addInform', async ctx => {
  let { tower,
    name,
    studentNum,
    inform } = ctx.request.body
  const addInform = new informtModel({
    tower,
    name,
    studentNum,
    inform
  })
  let code
  try {
    await addInform.save()
    code = 0
  } catch (e) {
    code = -1
  }
  ctx.body = {
    msg: '添加成功',
    meta: {
      code: code === 0 ? 200 : code
    }
  }
})
// 查看通知
router.get('/findInform', async ctx => {
  let { tower, limit } = ctx.query
  let year = new Date().getFullYear()
  let where = {}
  let options = {}
  if (limit === 'all') {
    where = {
      tower
    }
  } else {
    if (Number(limit)) {
      where = {
        tower,
        time: {
          "$gte": new Date(year + ''),
          "$lte": new Date(`${year}-12-31`)
        },
      }
      options = {
        limit: Number(limit)
      }
    }
    where = {
      tower,
      time: {
        "$gte": new Date(year + ''),
        "$lte": new Date(`${year}-12-31`)
      }
    }
  }
  let result = await informtModel.find(where, null, options).sort({ "time": -1 })
  ctx.body = {
    result,
    meta: {
      code: 200
    }
  }

})
// 删除错误通知
router.post('/dropInform', async ctx => {
  const where = { _id: ctx.request.body.id }
  let code
  try {
    await informtModel.remove(where, null)
    code = 200
  } catch (e) {
    code = -1
  }
  ctx.body = {
    meta: {
      msg: '删除成功',
      code: code
    }
  }
})
export default router